#include "MarvelGammaPos.hh"

MarvelGammaPos::MarvelGammaPos(): MyBasePos()
{
    m_SDRadius = 20.; // unit: mm
}

MarvelGammaPos::~MarvelGammaPos()
{
    if(!m_HistFile)
        delete m_HistFile;
}

void MarvelGammaPos::Initialize(YAML::Node yamlNode)
{
    m_SDRadius = yamlNode["SDRadius"].as<double>(7.5) * mm;
    std::cout << "SDRadius: " << m_SDRadius << std::endl;

    m_HistFile = new TFile(yamlNode["HistFile"].as<std::string>("GammaAngular.root").c_str());
    m_PositionHist = dynamic_cast<TH2F*>(m_HistFile->Get(yamlNode["PosHistName"].as<std::string>("GammaPosition").c_str()));
    m_MomDirHist = dynamic_cast<TH2F*>(m_HistFile->Get(yamlNode["MomHistName"].as<std::string>("GammaMomDir").c_str()));
}

std::pair<G4ThreeVector, G4ThreeVector> MarvelGammaPos::GenPosAndMomDir()
{
    Double_t theta, phi;
    m_PositionHist->GetRandom2(theta, phi);
    G4ThreeVector position;
    position.setRThetaPhi(m_SDRadius, theta, phi);

    Double_t ptheta, pphi;
    m_MomDirHist->GetRandom2(ptheta, pphi);
    G4ThreeVector momDir;
    momDir.setRThetaPhi(1., ptheta, pphi);

    PosAndMom PosAndMomDir;
    PosAndMomDir.first = position;
    PosAndMomDir.second = momDir;
    return PosAndMomDir;
}

REGISTPOSGEN(MarvelGammaPos)